package com.czy.owner.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.entity.PunchRecordModel;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.TimeUtils;
import com.easyrecycleview.EasyRecyclerView;

import java.text.SimpleDateFormat;
import java.util.List;



/**
 * Created by wangyang on 2017/4/20 0020.
 */

public class PunchRecordAdapter extends BaseRecyclerAdapter<PunchRecordModel, PunchRecordAdapter.PunchRecordViewHolder> implements RecyclerHeadersAdapter<RecyclerView.ViewHolder> {
    private Context context;

    public PunchRecordAdapter(Context context, List<PunchRecordModel> data) {
        super(context, data);
        this.context = context;

    }

    @Override
    public PunchRecordViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.item_punch_record, parent, false);
        return new PunchRecordViewHolder(view);
    }

    @Override
    public void onBindViewHolder(PunchRecordViewHolder holder, int position) {
        holder.bindData(position);
    }


    @Override
    public long getHeaderId(int position) {
        return data.get(position).getSort();
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.header_item, parent, false);
        return new RecyclerView.ViewHolder(view) {
        };
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
        TextView tv = (TextView) holder.itemView.findViewById(R.id.tv_recycler_header);
        String localData = TimeUtils.formatTimeDifference5(System.currentTimeMillis());
        MyLog.e("RAVEN","adapter:localData=="+localData);
        MyLog.e("RAVEN","adapter:groupName=="+data.get(position).getMyGroupName());
        if(localData.equals(data.get(position).getMyGroupName())){
            tv.setText("本月");
        }else {
            tv.setText(data.get(position).getMyGroupName());
        }

    }

    public class PunchRecordViewHolder extends BaseRecyclerAdapter.ViewHolder {
        private LinearLayout ll_circle;
        private TextView tv_punch_record_time;
        private TextView tv_punch_record_type;
        private TextView tv_order_number;
        private TextView tv_car_name;
        private PunchRecordSubAdaper adapter = null;
        private EasyRecyclerView recyclerView_sub;

        public PunchRecordViewHolder(View itemView) {
            super(itemView);
            ll_circle = (LinearLayout) itemView.findViewById(R.id.ll_circle);
            tv_punch_record_time = (TextView) itemView.findViewById(R.id.tv_punch_record_time);
            tv_punch_record_type = (TextView) itemView.findViewById(R.id.tv_punch_record_type);
            tv_order_number = (TextView) itemView.findViewById(R.id.tv_order_number);
            tv_car_name = (TextView) itemView.findViewById(R.id.tv_car_name);
            recyclerView_sub = (EasyRecyclerView) itemView.findViewById(R.id.recyclerView_sub);
        }

        public void bindData(final int position) {
            String orderType = data.get(position).getOrderType();
            String borrowedFlag = data.get(position).getBorrowedFlag();
            if (orderType.equals("scratchCard")) {
                if(borrowedFlag.equals("true")){
                    ll_circle.setBackgroundResource(R.drawable.package_top_green);
                    tv_punch_record_type.setText("借卡");
                }else {
                    ll_circle.setBackgroundResource(R.drawable.package_top_blue);
                    tv_punch_record_type.setText("划卡");
                }

            } else if(orderType.equals("serviceOrder")){
                if(borrowedFlag.equals("true")){
                    ll_circle.setBackgroundResource(R.drawable.package_top_green);
                    tv_punch_record_type.setText("借卡");
                }else {
                    ll_circle.setBackgroundResource(R.drawable.package_top_blue);
                    tv_punch_record_type.setText("工单");
                }

            }
            tv_punch_record_time.setText(formatTimeMMDD(data.get(position).getEndTime()));
            tv_order_number.setText("订单号: " + data.get(position).getOrderNumber());

            if(borrowedFlag.equals("true")){
                tv_car_name.setText("借卡人: "+data.get(position).getNickName() + "  "+data.get(position).getPhone()+"  "+(TextUtils.isEmpty(data.get(position).getChName())?"":data.get(position).getChName())+" "+data.get(position).getLicenseNumber());
            }else {
                if (!TextUtils.isEmpty(data.get(position).getChName())) {
                    tv_car_name.setText(data.get(position).getChName());
                }
            }

            recyclerView_sub.setLayoutManager(new LinearLayoutManager(context));
            recyclerView_sub.setAdapter(adapter = new PunchRecordSubAdaper(context));
            adapter.addAll(data.get(position).getOrderItems());
        }
    }

    public static String formatTimeMMDD(long timeStamp) {
        String formatDate = "MM-dd";
        SimpleDateFormat dateFormat = new SimpleDateFormat(formatDate);
        String tmpDateStr = dateFormat.format(timeStamp);
        return tmpDateStr;

    }
}
